package com.finger.hospservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.finger.hospservice.repository.DepartmentRepository;
import com.finger.hospservice.service.DepartmentService;
import com.finger.modelservice.model.hosp.Department;
import com.finger.modelservice.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {

        String paramMapStr = JSONObject.toJSONString(paramMap);

        Department department = JSONObject.parseObject(paramMapStr, Department.class);

        String hoscode = department.getHoscode();

        String depcode = department.getDepcode();

        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

        if(departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setBigcode(department.getBigcode());
            departmentExist.setDepname(department.getBigname());

            departmentRepository.save(departmentExist);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);

        }


    }

    @Override
    public Page<Department> pageDepartment(int page, int size, String hoscode) {

        Pageable pageable = PageRequest.of(page-1,size);

        Department department = new Department();

        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if(department!=null){
            department.setUpdateTime(new Date());
            department.setIsDeleted(1);
            departmentRepository.save(department);
        }
    }

    @Override
    public List<DepartmentVo> findDepartmentTree(String hoscode) {

        List<DepartmentVo> result = new ArrayList<>();

        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        // 根据每个大科室编号分组，key是每个大科室的编号，value是根据大科室编号分组之后的集合
        Map<String, List<Department>> collect = departmentRepository.findAll(example).stream().collect(Collectors.groupingBy(Department::getBigcode));

        for(Map.Entry<String,List<Department>> entry : collect.entrySet()){

            // 大科室编号
            String bigCode = entry.getKey();

            // 根据 大科室编号分组
            List<Department> value = entry.getValue();

            DepartmentVo department1 = new DepartmentVo();

            department1.setDepname(value.get(0).getBigname());
            department1.setDepcode(bigCode);

            List<DepartmentVo> children = new ArrayList<>();

            for (Department department : value) {

                DepartmentVo department2 = new DepartmentVo();
                department2.setDepcode(department.getDepcode());
                department2.setDepname(department.getDepname());
                children.add(department2);
            }

            department1.setChildren(children);
            result.add(department1);

        }
        return result;
    }

    @Override
    // 根据医院编号和科室编号查找科室名称
    public String findDepName(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
